package hn.cch.coroutines

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.lang.Exception


class MainRun() {



}


fun main() = runBlocking {
    val job = launch(Dispatchers.Default) {
        repeat(5) { i: Int ->
            try {
                println("repeat:$i")
            } catch (e: Exception) {
                println(e)
            } finally {
                withContext(NonCancellable){
                    println("cancel")
                }
            }
        }
    }

    // job.join()
    job.cancelAndJoin()

}


suspend fun execute() = coroutineScope {
    launch {
        delay(1000L)
        println("launch")
    }
    println("execute")

}